#include <iostream>
#include <cstdio>
#include <stack>
#include <algorithm>
#include <vector>
#include <string>
#include <cstring>

using namespace std;
int n,t,m;
int mp[20][20];
int visited[20];
int ans=-1;


void dfs(int v,int dist){
    //cout << v << " " << dist <<endl;
    int flag=0;
    for(int i=0;i<m;i++){
        if(!visited[i]){
            flag=1;break;
        }
    }
    if(0==flag){
        if(ans==-1){
            ans=dist;
        }else{
            ans=min(ans,dist);
        }
    }

    for(int i=0;i<m;i++){
        if(!visited[i]&&mp[v][i]!=-1){
          
            visited[i]=1;
            dfs(i,dist+mp[v][i]);
            visited[i]=0;
        }
    }
}


int main(){
    freopen("1.in","r",stdin);
    int a=1e9;
    cout << a <<endl;
    memset(mp,-1,sizeof(mp));
    memset(visited,0,sizeof(visited));
    cin >> m >> t >> n;
    for(int i=0;i<n;i++){
        int a,b,c;
        cin >> a>> b >> c;
        a-=t;b-=t;
        mp[a][b]=mp[b][a]=c;
    }
    visited[0]=1;
    dfs(0,0);
    cout << ans <<endl;
    return 0;
}